<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class ExpertsController extends AppController {

		
	public $uses = array('Expert');

	/**
	 * Trang chu user
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function index() {
		App::uses('Category', 'Model');
		$Category = new Category();
		
		$als = &$this->als;
		
		if(!empty($this->request->data)) {
			$data = &$this->request->data[$als];
			$this->Expert->set($data);
			
			$capcha = $this->Session->read('send-contact');
			if($capcha != $data['captcha']) {
				$this->Expert->validationErrors['captcha'] = array('Id captcha incorect');
			}
			
			$errors = $this->Expert->invalidFields();
			if( empty($errors)) {
				$data['status'] = 0;
			
				if($this->Expert->save($data, false)) {
					$view = new View( $this );
					$Text = $view->loadHelper( 'Text' );
					$data['email'] = $Text->autoLink($data['email'], array( 'target' => '_blank'));
					
					App::uses('Codetbl', 'Model');
					$CT = new Codetbl();
					$info = $CT->getInfo();
					
					$mail_data = array(
						'data' => $data,
						'to' => $info['c']['value'],
						'subject' => __('Ask Expert'),
						'type' => 'html',
						'template' => 'ask-expert'
					);
					
					$this->Common->send_mail($mail_data);
					
					$data = null;
					
					$this->Session->setFlash(__('Send success.'), 'success');
				} else {
					$this->Session->setFlash(__('Send Fail.'), 'error');				
				}
			}
		}
		
		$category = $Category->getInfoByType('askexpert');
		$this->set('category', $category);
		
		// Set Website title
		$this->set('title_for_layout' , $category['Category']['web_title']);
	}
	
	/**
	 * Danh sách câu hoi
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_index(){
		if(!empty($this->request->data)) {
			$data = &$this->request->data;
			if(!empty($data['del'])) {
				foreach($this->request->data['del'] as $id => $v){
					if($v){
						// Trường hợp xóa
						if($data['Expert']['status'] == 9) {
							$this->Expert->delete((int)$id);
						} else {
							$this->Expert->id = (int)$id;
							$this->Expert->saveField('status', (int)$data['Expert']['status']);
						}
					}
				}
				$this->Session->setFlash('Delete success.', 'success');
			} else {
				$this->Session->setFlash('Delete fail', 'error');
			}
		}
		
		$this->paginate = array( 'order'=> array('status' => 'asc'));
		$items = $this->paginate();
		if(!empty($items))
			foreach($items as $k => $v) {
				if(!empty($v[$this->als]['created']))
					$v[$this->als]['created'] = @date('d/M/Y', $v[$this->als]['created']);
				$items[$k] = $v;
			}
		$this->set('items', $items);
		
		// thong tin hien thi cho trang
		$this->set('main_page_title', __('Ask Expert'));
		$this->set('main_page_description', __('Manager Ask Expert'));
		
		$breadcrumb = array( array('name' => 'List ask', 'url' => '') );
		$this->set('breadcrumb', $breadcrumb);
	}
	
	/**
	 * Xem chi tiet
	 *
	 * @param type  meta
	 * @return type  meta
	 * @access public
	 */
	function admin_detail($id = null) {
		$this->set('info', $this->Expert->read(array(), (int)$id));
		
		// thong tin hien thi cho trang
		$this->set('main_page_title', __('Ask Expert'));
		$this->set('main_page_description', __('Manager Ask Expert'));
		
		$breadcrumb = array(
			array('name' => 'Danh sách câu hỏi', 'url' => 'experts'),
			array('name' => 'Xem Chi tiết')
		);
		$this->set('breadcrumb', $breadcrumb);
	}
}
